package wechat.forward.Util;

import android.os.Environment;

import com.google.gson.Gson;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

import wechat.forward.Model.SettingModel;


/**
 * Created by xuxingcan on 16-10-24.
 */
public class FileUtil {
    private static volatile FileUtil instance = null;
    private static final File mSettingFile = new File(Environment.getExternalStorageDirectory(), "wechat.forwarder.setting");
    private Gson gson;
    private SettingModel mSettingModel;

    public static FileUtil getInstance() {
        if (instance == null) {
            synchronized (FileUtil.class) {
                if (instance == null) {
                    instance = new FileUtil();
                }
            }
        }
        return instance;
    }

    private FileUtil() {
        gson = new Gson();
        reloadSetting();
    }

    public boolean writeSetting(SettingModel model) {
        try {
            String s = gson.toJson(model);
            writeFileSdcardFile(mSettingFile, s);
            mSettingModel = model;
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public SettingModel getSettingModel() {
        return mSettingModel;
    }

    public SettingModel reloadSetting() {
        try {
            String s = readFileSdcardFile(mSettingFile);
            mSettingModel = gson.fromJson(s, SettingModel.class);
        } catch (Exception e) {
            e.printStackTrace();
            if (mSettingModel == null) {
                mSettingModel = new SettingModel();
            }
        }
        return mSettingModel;
    }

    //写数据到SD中的文件
    public static void writeFileSdcardFile(File path, String write_str) throws Exception {
        FileOutputStream fout = new FileOutputStream(path);
        byte[] bytes = write_str.getBytes();
        fout.write(bytes);
        fout.close();
    }


    //读SD中的文件
    public static String readFileSdcardFile(File path) throws Exception {
        String res = "";
        FileInputStream fin = new FileInputStream(path);
        int length = fin.available();
        byte[] buffer = new byte[length];
        fin.read(buffer);
        res = new String(buffer, "UTF-8");
        fin.close();
        return res;
    }

    public static String getFileMD5(File file) {
        if (!file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    }
}
